<?php
	require_once("Member.php");
	require_once("BoatHandler.php");
	require_once("MemberView.php");
	require_once("ListView.php");
	require_once("NavigationView.php");
	require_once("BoatView.php");
	
   	class NavigationController {
		private $m_member;
		private $m_boatHandler;
   		public function __construct(Member $member, BoatHandler $boatHandler){
			
			$this->m_member = $member;
			$this->m_boatHandler = $boatHandler;
		}
		
		public function DoControll() {
			$xhtml = "<h2>Workshop 2</h2>";
			
			$memberView = new MemberView();
			$listView = new ListView();
			$boatView = new BoatView();
			$navigationView = new NavigationView();

				
			if ($navigationView->TriedToRegisterMember() == TRUE) {
			 	$xhtml .= $memberView->DoRegisterView();
			}
			elseif ($navigationView->TriedToGenerateCompactList() == TRUE) {
				$xhtml .= $listView->GenerateMemberList($this->m_member->GetStaticMemberList());
			}
			elseif ($navigationView->TriedToGenerateFullList() == TRUE) {
				$xhtml .= $listView->GenerateMemberList($this->m_member->GetFullMemberList());
			}
			elseif ($navigationView->TriedToDeleteMember() == TRUE ) {
				$xhtml .= $memberView->DoDelete();
			}
			elseif ($navigationView->TriedToEditMember() == TRUE) {
				$xhtml .= $memberView->DoUpdateView();
			}
			elseif ($navigationView->TriedToViewMember() == TRUE) {
				$xhtml .= $memberView->DoViewOneMember();
			}
			elseif ($navigationView->TriedToAddBoatInfo() == TRUE) {
				$xhtml .= $boatView->DoAddBoatInfoView();
			}
			elseif ($navigationView->TriedToDeleteBoat() == TRUE) {
				$xhtml .= $boatView->DoDeleteBoatView();
			}
			elseif ($navigationView->TriedToEditBoat() == TRUE) {
				$xhtml .= $boatView->DoEditBoatView();
			}
			else {
				$xhtml = $navigationView->ShowStartButtons(); 
			}
						
			if($memberView->TriedToRegister()) {
				try {
					$this->m_member->CreateMember($memberView->GetSsn(), $memberView->GetFirstName(), $memberView->GetLastName());
				 } catch (Exception $e) {
					throw new Exception ("Något gick fel vid CreateMember");
				 }
			 }
		
			if($memberView->TriedToDelete()) {
				try {
					
					 if($this->m_member->DeleteMember($memberView->GetMemberId()) == true) {
					 }
				} catch (Exception $e) {
					throw new Exception("Något gick fel vid DeleteMember");		
				}
			}
			
			if($memberView->TriedToUpdate()) {
				try {
					
					 $this->m_member->ChangeMember($memberView->GetMemberId(),
					 								$memberView->GetFirstName(),
													$memberView->GetLastName(),
													$memberView->GetSsn());
				} catch (Exception $e) {
					throw new Exception("Något gick fel vid ChangeMember");		
				}
			}
			
			if($boatView->TriedToAddBoatInfo()) {
				try {
					
					 $this->m_boatHandler->AddBoatInfo($boatView->GetMemberId(),
					 								$boatView->GetBoatType(),
													$boatView->GetBoatLength());
				} catch (Exception $e) {
					throw new Exception("Något gick fel vid AddBoatInfo");		
				}
			}
			
			if ($boatView->TriedToDeleteBoat()) {
				try {
					
					$this->m_boatHandler->DeleteBoatInfo($boatView->GetBoatId());
					
				} catch (Exception $e) {
					throw new Exception("Något gick fel vid DeleteBoatInfo");
				}
			}
			
			if ($boatView->TriedToEditBoat()) {
				try{
						
					$this->m_boatHandler->ChangeBoatInfo($boatView->GetBoatId(), 
														$boatView->GetBoatType(), 
														$boatView->GetBoatLength());
														
				}catch(Exception $e){
					throw new Exception("Något gick fel vid ChangeBoatInfo");
				}
			}
			
			
			if ($memberView->TriedToView()) {
				try{	
					$xhtml .= $listView->GenerateMemberList($this->m_member->GetMemberById($memberView->GetMemberId()));				
				} catch (Exception $e) {
					throw new Exception("Något gick fel vid GenerateMemberList");	
				}
			}
			return $xhtml;
		}
   	}
?>